package project.ia04.agents.gui;

import jade.core.behaviours.Behaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import project.ia04.gui.components.StaffListChooserDialog;
import project.ia04.gui.frames.MainFrame;
import project.ia04.utils.Data;

public class XAgentGetStaffsBehaviour extends Behaviour {
	
	private int state = 0;
	private Object source;
	
	public XAgentGetStaffsBehaviour(Object src){
		this.source = src;
	}

	@Override
	public void action() {
		ACLMessage message;
		if(state ==0){
			System.out.println("XAgent --> XAgentGetStaffsBehaviour (Send)");
			message = new ACLMessage(ACLMessage.REQUEST);
			message.setConversationId(Data.GETSTAFFSLIST);
			message.addReceiver(((XAgent)myAgent).getEnvAgent());
			myAgent.send(message);
			state++;
		}
		else if(state == 1){
			MessageTemplate template = MessageTemplate.and(MessageTemplate.MatchPerformative(ACLMessage.INFORM),
										MessageTemplate.MatchConversationId(Data.GETSTAFFSLIST));
			message = myAgent.receive(template);
			if(message != null){
				System.out.println("XAgent --> XAgentGetStaffsBehaviour (Reception)");
				if(source instanceof StaffListChooserDialog)
					((StaffListChooserDialog)source).add2List(((XAgent)myAgent).toStaffList(message.getContent()));
				else if(source instanceof MainFrame)
					((XAgent)myAgent).send2Frame(Data.GETSTAFFSLIST,message.getContent());
				state++;
			}
			else
				block();
		}
	}

	@Override
	public boolean done() {
		return state == 2;
	}

}
